//
//  ZZZXGoodsAnalysisVC.m
//  新项目
//
//  Created by liGordon on 17/3/8.
//  Copyright © 2017年 刘耀宗. All rights reserved.
//
#import "ZZZXGoodsAnalysisVC.h"
#import "ZZZXGoodsAnalysisCell.h"
#import "ZZZXGoodsAnalysisDetailVC.h"
#import "ZZDataFXViewModel.h"
static NSString * identifier = @"ZXGoodsAnalysis";
@interface ZZZXGoodsAnalysisVC ()<UITableViewDataSource,UITableViewDelegate,ZZZXGoodsAnalysisCellDelegate>
@property (weak, nonatomic) IBOutlet UIButton *searchBtn;
@property (weak, nonatomic) IBOutlet UITableView *menuTable;
@property (weak, nonatomic) IBOutlet UIView *inputView;
@property (weak, nonatomic) IBOutlet UITextField *dayField;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, copy) NSString * num;
@property (nonatomic, strong) CcNoDataView *noDataView;
@property (nonatomic, assign) NSInteger index;

@end

@implementation ZZZXGoodsAnalysisVC
-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;

}
-(CcNoDataView *)noDataView
{
    if (_noDataView == nil) {
        _noDataView = [[CcNoDataView alloc] init];
        _noDataView.backgroundColor=[UIColor whiteColor];
    }
    return _noDataView;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    self.menuTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.inputView.layer.masksToBounds = YES;
    self.inputView.layer.cornerRadius = 8;
    self.searchBtn.layer.masksToBounds = YES;
    self.searchBtn.layer.cornerRadius = 8;
    self.title = @"滞销商品分析";
    self.num=@"0";
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.index=1;
        
           [self seletecdDataWIthIndex:[NSString stringWithFormat:@"%ld",self.index]];
            [self.menuTable.mj_footer  resetNoMoreData];
    }];
    self.menuTable.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.total>upNum*self.index) {
            self.index++;
            [self seletecdDataWIthIndex:[NSString stringWithFormat:@"%ld",self.index]];
        }
        else
        {
            [self.menuTable.mj_footer endRefreshingWithNoMoreData];
            
        }
        
    }];
    
    
    self.menuTable.mj_header=header;
    [self.menuTable.mj_header beginRefreshing];
 
    
}
- (IBAction)clickForSeletecd:(id)sender {
    self.num=self.dayField.text;
    [self.menuTable.mj_header beginRefreshing];
    
    
}
-(void)seletecdDataWIthIndex:(NSString *)index
{
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];

    [[ZZDataFXViewModel shareViewModel] DataFenXiWithunsalableDay:self.num andsTime:@"" andEtime:@"" andsaleNum:@"0" andpartsName:@"" andsupplierName:@"" andcompanyID:loginDict[@"CompanyID"] andcompanyFID:loginDict[@"CompanyFID"] andpageIndex:index andpageSize:@(upNum) andflag:@"UnsalableGoods" andSuccess:^(NSDictionary *result) {
        NSLog(@"res=%@",result);
           [self.menuTable.mj_header endRefreshing];
        if (result) {
            NSDictionary *resDicd=[WHJSON objectFormJsonString:result[@"resultJson"]];
            NSLog(@"当前字典是%@",resDicd);
            NSMutableArray *mutableArr=[NSMutableArray array];
            for (NSDictionary *dic in resDicd[@"rows"]) {
                [mutableArr addObject:dic];
            }
            if (self.index!=1) {
                if (mutableArr.count==0) {
                    [self.menuTable.mj_footer endRefreshingWithNoMoreData];
                    return ;
                }
                [self.dataArr addObjectsFromArray: mutableArr];
                [self.menuTable.mj_footer endRefreshing];
                
            }
            else
            {
                self.dataArr=mutableArr;
                [self.menuTable.mj_header endRefreshing];
                
            }

            
            
            
            
            
            
            [self.noDataView dataIsEmpty:self.dataArr withView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 130, newwidth, newheight-130) andBtnHiddne:YES];
            self.noDataView.label.text=@"暂无数据";
            
            self.total=[resDicd[@"total"] integerValue];
            [self.menuTable reloadData];
         
        }
       
        
        
        
    } andFailer:^{
        [self.menuTable.mj_header endRefreshing];
        [self.noDataView noNetwithView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 130, newwidth, newheight-130) andBtnHiddne:NO];
        //点击重新加载的block
        WEAKSELF
        self.noDataView.myBlock=^{
            [weakSelf.menuTable.mj_header beginRefreshing];
        };
        
    }];

}

- (void)setupTableView
{
    _menuTable.delegate = self;
    _menuTable.dataSource = self;
    _menuTable.rowHeight = 70;
    [_menuTable registerNib:[UINib nibWithNibName:@"ZZZXGoodsAnalysisCell" bundle:nil] forCellReuseIdentifier:identifier];
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZZZXGoodsAnalysisCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dic=self.dataArr[indexPath.row];
    cell.delegate  = self;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZZZXGoodsAnalysisDetailVC *vc = [[ZZZXGoodsAnalysisDetailVC alloc]init];
    vc.dic=self.dataArr[indexPath.row];
    vc.flag=1;
    PushVC(vc)

}
-(void)clickDetail
{
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

@end
